<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="handleAdd('0',1 )">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button v-if="row.type=== 1" size="small" type="text" @click="handleAdd(row.id, 2)">添加权限点</el-button>
              <el-button size="small" type="text" @click="toEdit(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="handleDelete(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增编辑的弹层 -->
    <el-dialog :visible="showDialog" :title="title" @close="handleClose">
      <!-- 表单内容 -->
      <el-form ref="addRef" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="是否展示" prop="enVisible">
          <el-switch v-model="formData.enVisible" active-value="0" inactive-value="1" active-text="启用" inactive-text="禁用" active-color="#13ce66" inactive-color="#eee" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqDelPermission, reqGetPermissionDetail, reqUpdatePermission } from '@/api/permisson.js'
import { tranlateListToTree } from '@/utils/index.js'
export default {
  name: 'Permission',
  data () {
    return {
      title: '添加权限',
      list: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      // 表单验证
      rules: {
        name: [
          { required: true, message: '权限点名称不能为空', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '权限点编码不能为空', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.loadPermList()
  },
  methods: {
    // 编辑
    async toEdit (id) {
      const ret = await reqGetPermissionDetail(id)
      this.formData = ret.data
      this.showDialog = true
      this.title = '编辑权限'
    },
    // 删除
    handleDelete (id) {
      this.$confirm('确认要删除吗？', '温馨提示').then(async () => {
        const ret = await reqDelPermission(id)
        if (ret.code === 10000) {
          this.loadPermList()
          this.$message.success('删除成功')
        }
      }).catch((e) => {
        if (e !== 'cancel') {
          this.$message.error('删除失败')
        } else {
          this.$message.error('取消删除')
        }
      })
    },
    // 关闭弹窗
    handleClose () {
      this.showDialog = false
      this.$refs.addRef.resetFields()
    },
    // 提交表单
    handleSubmit () {
      this.$refs.addRef.validate(async valid => {
        if (!valid) return
        if (this.formData.id) {
          //  编辑
          try {
            const ret = await reqUpdatePermission(this.formData)
            if (ret.code === 10000) {
              this.showDialog = false
              this.$refs.addRef.resetFields()
              this.loadPermList()
            }
          } catch {
            this.$message.error('编辑权限点失败')
          }
        } else {
          // 添加
          // 添加权限
          try {
            const ret = await reqAddPermission(this.formData)
            if (ret.success) {
              // 关闭弹窗
              // this.showDialog = false
              // 刷新列表
              this.loadPermList()
              // 清空表单
              // this.$refs.authForm.resetFields()
              // this.formData = this.$options.data().formData
              this.handleClose()
            } else {
              this.$message.error(ret.message)
            }
          } catch {
            this.$message.error('新增权限点失败')
          }
        }
      })
    },
    // 添加弹窗动作
    handleAdd (pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },

    // 获取权限列表
    async loadPermList () {
      try {
        const ret = await reqGetPermissionList()
        // console.log(ret)
        if (ret.code === 10000) {
          this.list = tranlateListToTree(ret.data, '0')
        } else {
          this.$message.error(ret.message)
        }
      } catch (err) {
        console.log(err)
        this.$message.error('获取权限列表失败')
      }
    }
  }
}
</script>
